package com.sunday.pinxinghui.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.entity.CartItem;
import com.sunday.pinxinghui.model.main.CartFragment;
import com.sunday.pinxinghui.model.main.IndexFragment;

import java.math.RoundingMode;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘涛 on 2017/8/18.
 */

public class CartAdapter extends RecyclerView.Adapter {

    private List<CartItem> dataSet;
    private Context mContext;
    private View.OnClickListener onClickListener;


    public CartAdapter(Context context, List<CartItem> datas) {
        this.mContext = context;
        this.dataSet = datas;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.list_cart_item, null);
        return new ListHolder(view);

    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final ListHolder cartHolder = (ListHolder) holder;
        final CartItem cartItem = dataSet.get(position);

        if (!TextUtils.isEmpty(cartItem.getProductImage())) {
            Glide.with(mContext)
                    .load(cartItem.getProductImage())
                    .placeholder(R.mipmap.ic_default)
                    .error(R.mipmap.ic_default)
                    .into(cartHolder.ivGoods);
        }
        cartHolder.ivCheckGood.setVisibility(View.VISIBLE);
        cartHolder.llGoodLeft.setVisibility(View.VISIBLE);
        cartHolder.productTitle.setText(cartItem.getProductName());
        cartHolder.productSpec.setText(String.format("规格：%s", cartItem.getNormal()));
        cartHolder.productPrice.setText(String.format("¥%s", cartItem.getPrice().setScale(2, RoundingMode.HALF_UP)));
        cartHolder.productOldPrice.setText(String.format("¥%s", cartItem.getCostPrice().setScale(2, RoundingMode.HALF_UP)));
        cartHolder.productOldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
        cartHolder.tvNum2.setText(String.valueOf(cartItem.getNum()));
        if (cartItem.isSelect()) {
            cartHolder.ivCheckGood.setImageResource(R.mipmap.cart_selected);
        } else {
            cartHolder.ivCheckGood.setImageResource(R.mipmap.cart_unselect);
        }

        switch (cartItem.getType()) {
            case 1:
                //普通商品
                cartHolder.flagImg.setVisibility(View.GONE);
                break;
            case 2:
                //秒杀产品
                cartHolder.flagImg.setVisibility(View.VISIBLE);
                cartHolder.flagImg.setImageResource(R.mipmap.miaosha);
                break;
            case 3:
                //限时购商品
                cartHolder.flagImg.setVisibility(View.VISIBLE);
                cartHolder.flagImg.setImageResource(R.mipmap.qianggou);
                break;
        }




        cartHolder.deleteImg.setTag(position);
        cartHolder.ivCheckGood.setTag(position);
        cartHolder.ivAdd.setTag(position);
        cartHolder.ivReduce.setTag(position);
        cartHolder.tvNum2.setTag(position);
        cartHolder.rlCartItem.setTag(position);

        cartHolder.deleteImg.setOnClickListener(onClickListener);
        cartHolder.ivCheckGood.setOnClickListener(onClickListener);
        cartHolder.ivAdd.setOnClickListener(onClickListener);
        cartHolder.ivReduce.setOnClickListener(onClickListener);
        cartHolder.rlCartItem.setOnClickListener(onClickListener);
        cartHolder.tvNum2.setOnClickListener(onClickListener);



    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }



    class ListHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.ivCheckGood)
        ImageView ivCheckGood;
        @Bind(R.id.ivGoods)
        ImageView ivGoods;
        @Bind(R.id.product_title)
        TextView productTitle;
        @Bind(R.id.product_spec)
        TextView productSpec;
        @Bind(R.id.ivReduce)
        ImageView ivReduce;
        @Bind(R.id.tvNum2)
        TextView tvNum2;
        @Bind(R.id.ivAdd)
        ImageView ivAdd;
        @Bind(R.id.llGoodLeft)
        LinearLayout llGoodLeft;
        @Bind(R.id.delete)
        ImageView deleteImg;
        @Bind(R.id.product_price)
        TextView productPrice;
        @Bind(R.id.product_old_price)
        TextView productOldPrice;
        @Bind(R.id.rl_cartItem)
        RelativeLayout rlCartItem;
        @Bind(R.id.flag_img)
        ImageView flagImg;



        public ListHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }




}
